<?php
/**
 * Created by VS code.
 * User: liuhong
 * Date: 2019-08-29
 * Time: 14:39
 */


namespace app\api\controller;

use app\common\model\Classes as ClassesModel;
use app\common\model\ClassesStu as ClassesStuModel;
use app\common\model\ClassKaoqinLog;
use app\common\model\Manager;
use app\common\model\ManagerGongzi;
use app\common\model\Student as StudentModel;
use app\common\model\StudentKaoqin;
use app\common\model\UserStudent;
use app\service\LogService;


class Classes extends Base
{

    public function __construct()
    {
        parent::__construct();
    }

    //查询班级列表
    public function list()
    {
        $classes = ClassesModel::getAll([]);
        $this->ajax(200, 'success', $classes);
    }

    //查询班级学员列表
    public function student()
    {
        //根据班级ID查询学员信息
        $where_sids = [
            'cid' => ['eq', input('cid')]
        ];
        $sids = ClassesStuModel::getColumn($where_sids, 'sid');
        $where = [
            'id' => ['in', $sids]
        ];
        $list = StudentModel::getAll($where, '', 'id,name');
        $this->ajax(200, 'success', $list);
    }

    /**
     * 班级学院考勤
     * @return \think\response\Json
     */
    public function kaoqin()
    {
        $input = input();
        if (!$input['content']) {
            $this->ajax(201, '请填写备注');
        }
        if (!$input['time']) {
            $this->ajax(201, '请选择上课时间');
        }
        if (!$input['thumb']) {
            $this->ajax(201, '请上传考勤图片');
        }
        if (!$input['sids']) {
            $this->ajax(201, '请选择学员');
        }
        $all = [];
        $time = time();
        foreach ($input['sids'] as $val) {
            $userStu = UserStudent::getOne(['sid' => $val]);
            if ($userStu) {
                $all[] = [
                    'uid' => $userStu['uid'],
                    'sid' => $val,
                    'create_time' => $time,
                    'update_time' => $time,
                    'content' => $input['content'],
                    'mall_id' => 1,
                    'manager_name' => $input['manager_name'],
                    'thumb' => $input['thumb'],
                    'time' => $input['time'],
                    'cid' => $input['cid']
                ];
            } else {
                $all[] = [
                    'uid' => 0,
                    'sid' => $val,
                    'create_time' => time(),
                    'update_time' => time(),
                    'content' => $input['content'],
                    'mall_id' => 1,
                    'manager_name' => $input['manager_name'],
                    'thumb' => $input['thumb'],
                    'time' => $input['time'],
                    'cid' => $input['cid']

                ];
            }
        }
        if ($input['buke_sids']) {
            foreach ($input['buke_sids'] as $val) {
                $userStu = UserStudent::getOne(['sid' => $val]);
                if ($userStu) {
                    $all[] = [
                        'uid' => $userStu['uid'],
                        'sid' => $val,
                        'create_time' => $time,
                        'update_time' => $time,
                        'content' => $input['content'],
                        'mall_id' => 1,
                        'manager_name' => $input['manager_name'],
                        'thumb' => $input['thumb'],
                        'time' => $input['time']
                    ];
                } else {
                    $all[] = [
                        'uid' => 0,
                        'sid' => $val,
                        'create_time' => time(),
                        'update_time' => time(),
                        'content' => $input['content'],
                        'mall_id' => 1,
                        'manager_name' => $input['manager_name'],
                        'thumb' => $input['thumb'],
                        'time' => $input['time']

                    ];
                }
            }
        }
        if ($all) {
            $log = [];
            foreach ($all as $value) {
                $d = StudentKaoqin::setInsertGetId($value);
                $student = studentModel::getOne(['id' => $value['sid']]);
                studentModel::setFieldInc(['id' => $value['sid']], 'has_class_hour');//消除课时
                $student1 = studentModel::getOne(['id' => $value['sid']]);
                $add['type'] = 'kaoqin';
                $add['info'] = $input['mid'];
                $add['data'] = json_encode(['input' => $input, 'student_old' => $student, 'student_new' => $student1]);
                $add['create_time'] = date('Y-m-d H:i:s');
                $add['api'] = $_SERVER['REQUEST_URI'];
                $add['remark'] = $value['sid'];
                $log[] = $add;
            }

            if ($log) {
                LogService::S()->writeLogs($log);
            }

        }
        $kaoqin = array(
            'cid' => $input['cid'],
            'mid' => $this->manager['id'],
            'update_time' => $time,
            'create_time' => $time,
            'time' => $input['time'],
            'thumb' => $input['thumb']
        );
        $r = ClassKaoqinLog::setInsertGetId($kaoqin);
        $g_where = array(
            'mid' => $this->manager['id'],
            'time' => $input['time'],
        );
        ManagerGongzi::gongziUpdate($g_where);
        $this->ajax(200, '考勤成功');
    }


}

?>